package my.ssma.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import my.ssma.dao.UserBaseMapper;
import my.ssma.domain.User;
import my.ssma.service.UserBaseService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserBaseServiceImpl implements UserBaseService {

    @Autowired
    private UserBaseMapper userBaseMapper;

    // 查询所有
    @Override
    public List<User> SelectList() {
        return userBaseMapper.selectList(null);
    }

    // findById
    @Override
    public User findById(Integer id) {
        return userBaseMapper.selectById(id);
    }

    // like
    @Override
    public List<User> nameLike(String name) {

        Wrapper<User> like = new Condition().like("username", name, SqlLike.LEFT);
        return userBaseMapper.selectList(like);
    }

    // 大于
    @Override
    public List<User> greatThen(Integer id) {

        Wrapper<User> like = new Condition().gt("id", id);
        return userBaseMapper.selectList(like);
    }

    /**
     * 分页查询
     * @param pageNum 数据库起始行
     * @param pageSize 偏移，每页几条数据
     * @return 默认重 pageNum + 1 开始查找数据
     */
    public List<User> pageable(Integer pageNum, Integer pageSize){

        if (pageNum <= 0) pageNum = 1;

        pageNum = (pageNum - 1)*pageSize;
        RowBounds rowBounds = new RowBounds(pageNum, pageSize);
        return userBaseMapper.selectPage(rowBounds, null);

    }

    public List<User> pageable2(Integer pageNum, Integer pageSize){

        Page<User> page = new Page<>(pageNum, pageSize);
        return userBaseMapper.selectPage(page, null);
    }

}
